package seeders

import (
	"game-server/app/common/enum"
	"game-server/app/models"
	"game-server/global"
	"game-server/utils"
)

func InitData() {
	//创建category
	categorys := []models.Category{
		{
			Name:   "分类1",
			Icon:   "https://image.yaksgames.com/v2/game/1/9/9/199f3b3ad0beb02165444e3402ce63cc.png",
			Desc:   "分类1",
			Status: enum.Enabled,
		},
		{
			Name:   "分类2",
			Icon:   "https://image.yaksgames.com/v2/game/1/9/9/199f3b3ad0beb02165444e3402ce63cc.png",
			Desc:   "分类2",
			Status: enum.Enabled,
		},
		{
			Name:   "分类3",
			Icon:   "Uhttps://image.yaksgames.com/v2/game/1/9/9/199f3b3ad0beb02165444e3402ce63cc.png",
			Desc:   "分类3",
			Status: enum.Enabled,
		},
	}
	// 创建category
	for _, category := range categorys {
		global.App.DB.Create(&category)
	}

	// 1. 创建超级管理员角色
	superAdminRole := &models.SysRole{
		Name:   "超级管理员",
		Code:   "super_admin",
		Sort:   1,
		Status: 1,
		Remark: "系统超级管理员",
	}
	global.App.DB.Create(superAdminRole)

	// 2. 创建超级管理员用户
	superAdmin := &models.SysUser{
		Username: "admin",
		Password: utils.BcryptMake([]byte("123456")),
		Nickname: "超级管理员",
		Mobile:   "13800138000",
		Status:   1,
		Type:     1, // 超级管理员类型
		Roles:    []models.SysRole{*superAdminRole},
	}
	global.App.DB.Create(superAdmin)

	// 3. 创建系统菜单
	menus := []models.SysMenu{
		{
			ParentId:  0,
			Name:      "系统管理",
			Path:      "/system",
			Component: "Layout",
			Sort:      1,
			Icon:      "setting",
			Type:      1, // 目录
			Status:    1,
		},
		{
			ParentId:  1,
			Name:      "用户管理",
			Path:      "/system/user",
			Component: "system/user/index",
			Sort:      1,
			Icon:      "user",
			Type:      2, // 菜单
			Status:    1,
			ApiPath:   "/gameServer/admin/user",
			Perms:     "system:user:list",
		},
		{
			ParentId:  1,
			Name:      "角色管理",
			Path:      "/system/role",
			Component: "system/role/index",
			Sort:      2,
			Icon:      "role",
			Type:      2,
			Status:    1,
			ApiPath:   "/gameServer/admin/role",
			Perms:     "system:role:list",
		},
		{
			ParentId:  1,
			Name:      "菜单管理",
			Path:      "/system/menu",
			Component: "system/menu/index",
			Sort:      3,
			Icon:      "menu",
			Type:      2,
			Status:    1,
			ApiPath:   "/gameServer/admin/menu",
			Perms:     "system:menu:list",
		},
		// 用户管理按钮
		{
			ParentId: 2,
			Name:     "用户查询",
			Sort:     1,
			Type:     3, // 按钮
			Status:   1,
			Perms:    "system:user:query",
		},
		{
			ParentId: 2,
			Name:     "用户新增",
			Sort:     2,
			Type:     3,
			Status:   1,
			Perms:    "system:user:add",
		},
		{
			ParentId: 2,
			Name:     "用户修改",
			Sort:     3,
			Type:     3,
			Status:   1,
			Perms:    "system:user:edit",
		},
		{
			ParentId: 2,
			Name:     "用户删除",
			Sort:     4,
			Type:     3,
			Status:   1,
			Perms:    "system:user:remove",
		},
		// 角色管理按钮
		{
			ParentId: 3,
			Name:     "角色查询",
			Sort:     1,
			Type:     3,
			Status:   1,
			Perms:    "system:role:query",
		},
		{
			ParentId: 3,
			Name:     "角色新增",
			Sort:     2,
			Type:     3,
			Status:   1,
			Perms:    "system:role:add",
		},
		{
			ParentId: 3,
			Name:     "角色修改",
			Sort:     3,
			Type:     3,
			Status:   1,
			Perms:    "system:role:edit",
		},
		{
			ParentId: 3,
			Name:     "角色删除",
			Sort:     4,
			Type:     3,
			Status:   1,
			Perms:    "system:role:remove",
		},
		// 菜单管理按钮
		{
			ParentId: 4,
			Name:     "菜单查询",
			Sort:     1,
			Type:     3,
			Status:   1,
			Perms:    "system:menu:query",
		},
		{
			ParentId: 4,
			Name:     "菜单新增",
			Sort:     2,
			Type:     3,
			Status:   1,
			Perms:    "system:menu:add",
		},
		{
			ParentId: 4,
			Name:     "菜单修改",
			Sort:     3,
			Type:     3,
			Status:   1,
			Perms:    "system:menu:edit",
		},
		{
			ParentId: 4,
			Name:     "菜单删除",
			Sort:     4,
			Type:     3,
			Status:   1,
			Perms:    "system:menu:remove",
		},
	}

	// 创建菜单
	for _, menu := range menus {
		global.App.DB.Create(&menu)
	}

	// 4. 为超级管理员分配所有菜单权限
	var allMenus []models.SysMenu
	global.App.DB.Find(&allMenus)
	global.App.DB.Model(superAdminRole).Association("Menus").Replace(allMenus)
}
